
#include <list>


#include <QtGui/qwidget.h>
#include <string>


#include "MyGraphClass.h"

#include "MyInterface.h"
//Q_IMPORT_PLUGIN(qjpeg)
//====================

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent) {
	QLabel *tempql;
        QLabel *ltext;
	QImage tempqi;
	QPushButton *tempb;
	//QPixmap test;
	int i,x,y;
	int the_width;
	double k;
	ostringstream fname;
	thisNode.resize(6);
        stringstream ss;

        InitialiseHistory();

        ltext=new QLabel(this);
        ltext->setText("Learning method:");
        thiscombolearn = new QComboBox(this);
        for(i=0;i<LAST_LEARN;i++){
            ss.str("");
            ss << LEARNING_STR(i);
            thiscombolearn->addItem(ss.str().data());
        }
        ltext->move(320,20);
        thiscombolearn->move(320,40);
        



	setFixedSize(W_WIN, H_WIN);
	
	//CHARGEMENT DU GRAPH
	thisGraph	=	new MyGraph();
        thisGraph->LoadXml("data","relationships.xml","weights.xml");
        //thisimgpath =   "data";

	thisGraph->SortNext();
	thisGraph->ShowVertices();
	
	
	
	k=THE_K;
	for(i=0;i<6;i++){
		//IMAGES
		tempql		=	new QLabel(this);
		thislabels.push_back(tempql);
		fname.str("");
		//fname << "" << i+1 << ".png";
                fname << thisGraph->getName(i);
		thisNode[i]	=	i+1;
		thisImages.push_back(fname.str());
		tempqi.load(tr(thisImages[i].data()));
		fname.str("");
		switch(i){
			case 1:
				x	=	SPACE_BORDER+2*WIDTH_IMG+SPACE_BETWEEN;
				y	=	SPACE_BORDER;
				the_width	=WIDTH_IMG-10;
				fname << "Proposed next";
				break;
			case 0:
				x	=	SPACE_BORDER;
				y	=	SPACE_BORDER;
				the_width	=WIDTH_IMG;
				fname << "Current";
				break;
			case 2:
				x	=	SPACE_BORDER;
				y	=	SPACE_BORDER+WIDTH_IMG+SPACE_BORDER+H_BUTTON+SPACE_BORDER;
				the_width	=floor(WIDTH_IMG/250*200);
				fname << "Image " << i;
				break;
			case 3:
				x	=	SPACE_BORDER+floor(WIDTH_IMG/250*200)+SPACE_BETWEEN;
				y	=	SPACE_BORDER+WIDTH_IMG+SPACE_BORDER+H_BUTTON+SPACE_BORDER;
				the_width	=floor(WIDTH_IMG/250*200*k);
				fname << "Image " << i;
				break;
			case 4:		
				x	=	SPACE_BORDER+floor(WIDTH_IMG/250*200)+SPACE_BETWEEN+floor(WIDTH_IMG/250*200*k)+SPACE_BETWEEN;
				y	=	SPACE_BORDER+WIDTH_IMG+SPACE_BORDER+H_BUTTON+SPACE_BORDER;
				the_width	=floor(WIDTH_IMG/250*200*pow(k,2));
				fname << "Image " << i;
				break;
			case 5:
				x	=	SPACE_BORDER+floor(WIDTH_IMG/250*200)+SPACE_BETWEEN+floor(WIDTH_IMG/250*200*k)+SPACE_BETWEEN+SPACE_BETWEEN+floor(WIDTH_IMG/250*200*pow(k,2));
				y	=	SPACE_BORDER+WIDTH_IMG+SPACE_BORDER+H_BUTTON+SPACE_BORDER;
				the_width	=floor(WIDTH_IMG/250*200*pow(k,3));
				fname << "Image " << i;
				break;
		}
		
		tempqi.scaledToWidth(the_width,Qt::SmoothTransformation);
		
		thislabels[i]->setPixmap(QPixmap::fromImage(tempqi.scaled(the_width,the_width,Qt::KeepAspectRatio,Qt::SmoothTransformation)));

	
		
		thislabels[i]->move(x,y);
		
		//BUTTON
		tempb		=	new QPushButton(tr(fname.str().data()), this);
		thisbuttons.push_back(tempb);
		thisbuttons[i]->setGeometry(SPACE_BORDER, SPACE_BORDER, 105, H_BUTTON);
		thisbuttons[i]->setFont(QFont("Times", 12, QFont::Bold));
		
		switch(i){
			case 1:
				//x	=	SPACE_BORDER+WIDTH_IMG+SPACE_BETWEEN+(int)floor(WIDTH_IMG/2)-52;
				x	=	SPACE_BORDER+2*WIDTH_IMG+SPACE_BETWEEN+(int)floor(WIDTH_IMG/2)-52;
				y	=	SPACE_BORDER+HEIGHT_IMG+SPACE_BETWEEN;
				connect(thisbuttons[i], SIGNAL(clicked()), this, SLOT(OnClick_1()));
				break;
			case 0:
				x	=	SPACE_BORDER+(int)floor(WIDTH_IMG/2)-52;
				y	=	SPACE_BORDER+HEIGHT_IMG+SPACE_BETWEEN;
				connect(thisbuttons[i], SIGNAL(clicked()), this, SLOT(OnClick_0()));
				break;
			case 2:
				x	=	SPACE_BORDER+(int)floor(WIDTH_IMG/2*200/250)-52;
				y	=	SPACE_BORDER+HEIGHT_IMG+SPACE_BORDER+H_BUTTON+SPACE_BORDER+HEIGHT_IMG+SPACE_BETWEEN+SECOND_LINE;
				connect(thisbuttons[i], SIGNAL(clicked()), this, SLOT(OnClick_2()));
				break;
			case 3:
				x	=	SPACE_BORDER+(int)floor(WIDTH_IMG*200/250)-52+(int)floor(WIDTH_IMG/2*200/250*THE_K)+SPACE_BETWEEN;
				y	=	SPACE_BORDER+HEIGHT_IMG+SPACE_BORDER+H_BUTTON+SPACE_BORDER+HEIGHT_IMG+SPACE_BETWEEN+SECOND_LINE;
				connect(thisbuttons[i], SIGNAL(clicked()), this, SLOT(OnClick_3()));
				break;
			case 4:
				x	=	SPACE_BORDER+(int)floor(WIDTH_IMG*200/250)-52+(int)floor(WIDTH_IMG*200/250*THE_K)+2*SPACE_BETWEEN+(int)floor(WIDTH_IMG/2*200/250*pow(THE_K,2));
				y	=	SPACE_BORDER+SPACE_BORDER+H_BUTTON+SPACE_BORDER+HEIGHT_IMG*2+SPACE_BETWEEN+SECOND_LINE;
				connect(thisbuttons[i], SIGNAL(clicked()), this, SLOT(OnClick_4()));
				break;
			case 5:		
				x	=	SPACE_BORDER+(int)floor(WIDTH_IMG*200/250)-52+(int)floor(WIDTH_IMG*200/250*THE_K)+3*SPACE_BETWEEN+(int)floor(WIDTH_IMG*200/250*pow(THE_K,2))+(int)floor(WIDTH_IMG/2*200/250*pow(THE_K,3));
				y	=	SPACE_BORDER+HEIGHT_IMG*2+SPACE_BORDER+H_BUTTON+SPACE_BORDER+SPACE_BETWEEN+SECOND_LINE;
				connect(thisbuttons[i], SIGNAL(clicked()), this, SLOT(OnClick_5()));
				break;
		}
		thisbuttons[i]->move(x,y);
		 
	}
        thissavew   =   new QPushButton("Save Weights", this);
        thissavew->move(320,80);
        connect(thissavew, SIGNAL(clicked()), this, SLOT(SaveWeights()));
	
}


void MyWidget::OnClick_0(){
	OnClick(0);
}
void MyWidget::OnClick_1(){
	OnClick(1);
}
void MyWidget::OnClick_2(){
	OnClick(2);
}
void MyWidget::OnClick_3(){
	OnClick(3);
}
void MyWidget::OnClick_4(){
	OnClick(4);
}
void MyWidget::OnClick_5(){
	OnClick(5);
}

void MyWidget::SaveWeights(){
    thisGraph->SaveWeights("out.xml");
    thisGraph->ShowVertices();
}

void MyWidget::OnClick(int i){
	int j,k;
	
	QImage tempqi;
	int current,the_width,method;
	ostringstream fname;
	
	
	cout << "clicked on button" << i <<  endl;
	

	
	cout << 0 << " : " << thisGraph->getNext(1,0) << endl;
	cout << 1 << " : " << thisGraph->getNext(1,1) << endl;
	cout << 2 << " : " << thisGraph->getNext(1,2) << endl;
	cout << 3 << " : " << thisGraph->getNext(1,3) << endl;


	if(i>1){
            for(k=0;k<LAST_LEARN;k++){
                if(LEARNING_STR(k)==thiscombolearn->currentText().toAscii().constData()){
                    method=k;
                    cout << LEARNING_STR(k) << endl;
                }
            }
            thisGraph->LearnXml(0,1,i,method);
	}
        ReloadHistory(thisImages[i]);
	
	for(j=0;j<6;j++){
		//cout << thisImages[j] << endl;
		//cout << thisNode[j] << endl;
		the_width	=floor(WIDTH_IMG/250*200);
		if(j==0){
			//cout << "B j==0" << endl;
			thisImages[j]	=	thisImages[i];
			thisNode[j]	=	thisNode[i];
			tempqi.load(tr(thisImages[j].data()));
			//cout << thisImages[j].data() << endl;
			thislabels[j]->setPixmap(QPixmap::fromImage(tempqi.scaled(WIDTH_IMG,WIDTH_IMG,Qt::KeepAspectRatio,Qt::SmoothTransformation)));
			//cout << "E j==0" << endl;
		}
		else{
			
			current		=	thisNode[0];
			thisNode[j]	=	thisGraph->getNext(current,j);
			//cout << "thisNode " << j  << " : " << thisNode[j] << endl;
			if(j==1){
				the_width	=	WIDTH_IMG;
			}
			else{
				the_width	=	floor(WIDTH_IMG/250*200)*pow(THE_K,j-1);
			}
			
			
			if(thisNode[j]!=-1){
				thisImages[j]	=	thisGraph->getName(thisNode[j]);
				//cout << "thisImage: " << thisImages[j].data() << endl;
				tempqi.load(tr(thisImages[j].data()));
				//cout << thisImages[j].data() << endl;
				thislabels[j]->setPixmap(QPixmap::fromImage(tempqi.scaled(the_width,the_width,Qt::KeepAspectRatio,Qt::SmoothTransformation)));
			}
			else{
				fname.str("");
				fname << "" << j << ".jpg";
				
				tempqi.load(tr(fname.str().data()));
				thislabels[j]->setPixmap(QPixmap::fromImage(tempqi.scaled(WIDTH_IMG,WIDTH_IMG,Qt::KeepAspectRatio,Qt::SmoothTransformation)));
			}
		}
	}
}


int MyWidget::InitialiseHistory(){
    int i;
    QLabel* temp;
    int the_width=80;
    string *temps;
    
    for(i=0;i<10;i++){
        temp    =   new QLabel(this);
        temps   =   new string;
        *temps   =   HISTORY_IMG;
        thishistory.push_back(temp);
        thishistory_names.push_back(*temps);
        
        thishistory[i]->move(i*the_width,530);
    }
    ReloadHistory(HISTORY_IMG);
    return(0);
}

int MyWidget::ReloadHistory(string top_name){
    int i;
    QImage tempqi;
    int the_width=80;

    list<string>::iterator it;


    //it  =   thishistory_names.begin();
    thishistory_names.push_back(top_name);
    thishistory_names.pop_front();
    it  =   thishistory_names.begin();

    for(i=0;i<thishistory.size();i++){
        cout << it->data() << endl;
        tempqi.load(it->data());

        tempqi.scaledToWidth(the_width,Qt::SmoothTransformation);
        thishistory[i]->setPixmap(QPixmap::fromImage(tempqi.scaled(the_width,the_width,Qt::KeepAspectRatio,Qt::SmoothTransformation)));
        it++;
    }
}
